package com.qdlc.p2p.biz.model;

import java.util.HashMap;
import java.util.Map;

import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.service.user.UserMobilePhoneService;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.PropertiesUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.UserModel;

public class AppUserModel extends UserModel {

	private Map<String, Object> data;

	private String phoneCode;// 注册时需要校验的手机验证码

	/**
	 * 判断用户是否已存在响应消息
	 * 
	 * @param count
	 * @return
	 */
	public Map<String, Object> checkUsernameResponse(int count) {
		data = new HashMap<String, Object>();
		if (count <= 0) {
			data.put("resCode", "1");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction12"));
		} else {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction08"));
		}
		return data;
	}

	/**
	 * 判断邮箱是否已被使用响应消息
	 * 
	 * @param count
	 * @return
	 */
	public Map<String, Object> checkEmailResponse(int count) {
		data = new HashMap<String, Object>();
		if (count <= 0) {
			data.put("resCode", "1");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction09"));
		} else {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction05"));
		}
		return data;
	}

	/**
	 * 判断身份证是否已被使用响应消息
	 * 
	 * @param count
	 * @return
	 */
	public Map<String, Object> checkCardIdResponse(int count) {
		data = new HashMap<String, Object>();
		if (count <= 0) {
			data.put("resCode", "1");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction11"));
		} else {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction07"));
		}
		return data;
	}

	/**
	 * 判断手机号是否被使用响应消息
	 * 
	 * @param count
	 * @return
	 */
	public Map<String, Object> checkMobilePhoneResponse(int count) {
		data = new HashMap<String, Object>();
		if (count <= 0) {
			data.put("resCode", "1");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction10"));
		} else {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction06"));
		}
		return data;
	}

	/**
	 * 邮箱参数非空校验
	 * 
	 * @return
	 */
	public Map<String, Object> validEmail() {
		if (StringUtil.isBlank(getEmail())) {
			data = new HashMap<String, Object>();
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction01"));
			return data;
		}
		return null;
	}

	/**
	 * 身份证参数非空校验
	 * 
	 * @return
	 */
	public Map<String, Object> validCardId() {
		if (StringUtil.isBlank(getCardId())) {
			data = new HashMap<String, Object>();
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction03"));
			return data;
		}
		return null;
	}

	/**
	 * 手机号非空校验
	 * 
	 * @return
	 */
	public Map<String, Object> validMobilePhone() {
		if (StringUtil.isBlank(getMobilePhone())) {
			data = new HashMap<String, Object>();
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction02"));
			return data;
		}
		return null;
	}

	/**
	 * 注册 响应信息
	 * 
	 * @throws Exception
	 */
	public Map<String, Object> registerResponse() throws Exception {
		UserService userService = (UserService) BeanUtil.getBean("userService");
		UserMobilePhoneService userMobilePhoneService = BeanUtil.getBean(UserMobilePhoneService.class);
		int count = userService.countByMobilePhone(getMobilePhone());
		if (count > 0) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppUserAction06"));
		} else {
			UserModel user = new UserModel();
			user.setUserType(getUserType());
			user.setPwd(getPwd());
			user.setUserName(getUserName());
			user.setInviteUser(getInviteUser());
			user.setMobilePhone(getMobilePhone());
			user.setPayPwd(getPwd());
			User u = userService.doRegister(user);
			if (user != null) {
				// 手机号认证更新
				userMobilePhoneService.modifyRegistMobilePhone(u, user.getMobilePhone());
			}
			data.put("resCode", "1");
			data.put("resMsg", PropertiesUtil.getValue("AppRegister05"));
		}
		return data;
	}

	/**
	 * 注册 检查参数有效性
	 */
	public Map<String, Object> validRegister() {
		data = new HashMap<String, Object>();
		if (StringUtil.isBlank(getMobilePhone())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppRegister01"));
			return data;
		}
		if (StringUtil.isBlank(getUserName())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppRegister02"));
			return data;
		}
		if (StringUtil.isBlank(getPwd())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppRegister03"));
			return data;
		}
		if (StringUtil.isBlank(getUserType())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppRegister04"));
		}
		return null;
	}

	public String getPhoneCode() {
		return phoneCode;
	}

	public void setPhoneCode(String phoneCode) {
		this.phoneCode = phoneCode;
	}

}
